//
//  MyDevicesVC.m
//  LazyHome
//
//  Created by yh on 2018/5/7.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "MyDevicesVC.h"
#import "Device8RelayCell.h"
#import "TCPClient.h"
#import "LHMessage.h"
#import <MJExtension/MJExtension.h>
#import "DeviceTCPManager.h"
#import "NSMutableData+BufferHandler.h"
#import "LHDeviceInfo.h"
#import "LHDeviceManger.h"
#import "DeviceCellHandle.h"
#import "DeviceCellProtocol.h"
#import "LHManager.h"

@interface MyDevicesVC ()<UITableViewDelegate, UITableViewDataSource,DeviceCellHandle>
@property (nonatomic, assign) unsigned char status;

@property (nonatomic, strong) NSMutableArray *devices;


@end

@implementation MyDevicesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCame:) name:NOTI_TCP_MESSAGE_CAME object:nil];
    [self setupTableView];
    self.devices = [LHDeviceManger shareManager].myDeviceIds;
    [[LHManager shareManager] queryDevices];
}

- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Device8RelayCell" bundle:nil] forCellReuseIdentifier:@"Device8RelayCell"];
}

#pragma -mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.devices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *deviceId = self.devices[indexPath.row];
    UITableViewCell<DeviceCellProtocol> *cell = [tableView dequeueReusableCellWithIdentifier:@"Device8RelayCell" forIndexPath:indexPath];
//   [cell setDataModel:deviceInfo handle:self];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setHandle:self];
    [cell setDeviceId:deviceId];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}


#pragma --mark DeviceCellHandle

- (void)device8RelayCell:(Device8RelayCell *)device8RelayCell changeDevice8RelayStatus:(unsigned char)status button:(UIButton *)button index:(int)index{
    [[LHManager shareManager] changeDeviceStatusWithDeviceId:device8RelayCell.deviceId deviceType:DEVICETYPE_8RELAY status:@{@"P2":@(status)}];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)messageCame:(NSNotification *)noti{
    LHMessage *msg = noti.object;
    NSString *string =[[NSString alloc]initWithData:msg.data encoding:NSUTF8StringEncoding];
    if ([msg.cmdData toUShort] == 0x2001) {
       
       
    }else if ([msg.cmdData toUShort] == 0x2002) {
        self.devices = [LHDeviceManger shareManager].myDeviceIds;
        [self.tableView reloadData];
    }
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
